﻿using System.Windows;
using System.Windows.Input;
using System.Collections.Generic;

namespace SilverlightGame
{
    /*
        Code originally from http://www.bluerosegames.com/silverlight-games-101/ 
    */
    public class KeyHandler
    {
        protected static KeyHandler instance = null;
        Dictionary<Key, bool> isPressed = new Dictionary<Key, bool>();
        FrameworkElement targetElement = null;

        public static KeyHandler Instance
        {
            get
            {
                if (instance == null)
                    instance = new KeyHandler();
                return instance;
            }
        }

        public void clearKeyPresses()
        {
            isPressed.Clear();
        }

        public KeyHandler()
        {
            
        }

        public void startupKeyHandler(FrameworkElement target)
        {
            clearKeyPresses();
            targetElement = target;
            target.KeyDown += new KeyEventHandler(target_KeyDown);
            target.KeyUp += new KeyEventHandler(target_KeyUp);
            target.LostFocus += new RoutedEventHandler(target_LostFocus);
        }

        public void shutdown()
        {
            clearKeyPresses();
            targetElement.KeyDown -= new KeyEventHandler(target_KeyDown);
            targetElement.KeyUp -= new KeyEventHandler(target_KeyUp);
            targetElement.LostFocus -= new RoutedEventHandler(target_LostFocus);
            targetElement = null;
        }

        void target_KeyDown(object sender, KeyEventArgs e)
        {
            if (!isPressed.ContainsKey(e.Key))
            {
                isPressed.Add(e.Key, true);
            }
        }

        void target_KeyUp(object sender, KeyEventArgs e)
        {
            if (isPressed.ContainsKey(e.Key))
            {
                isPressed.Remove(e.Key);
            }
        }

        void target_LostFocus(object sender, RoutedEventArgs e)
        {
            clearKeyPresses();
        }

        public bool isKeyPressed(Key k)
        {
            return isPressed.ContainsKey(k);
        }
    }
}